import Vue from 'vue'
import App from './App'
import router from './router'
import { Message ,DatePicker,Pagination,Loading,Dialog,Button,Input,Select,Option,MessageBox,Switch,Checkbox,Autocomplete,Form,FormItem} from 'element-ui'
import axios from 'axios'
import UUID from 'uuid-js'
// import Mock from './mock/mock'
// Mock.mockData()

/*
    图片上传，获取验证码两个接口不使用根路径
*/
// axios.defaults.baseURL = 'https://sales.huoxingy.com/api/v1/sales/';

// 根据环境来选择api地址
let baseUrl = window.location.hostname;
console.log(baseUrl)
switch (baseUrl) {
    case "localhost":
        axios.defaults.baseURL = 'https://staging-sales-api.huoxingy.com/api/v1/sales/';
        break;
    case "192.168.0.105":
        axios.defaults.baseURL = 'https://staging-sales-api.huoxingy.com/api/v1/sales/';
        break;
    case "staging-sales.huoxingy.com":
        axios.defaults.baseURL = 'https://staging-sales-api.huoxingy.com/api/v1/sales/';
        break;
    case "prod-test-sales.huoxingy.com":
        axios.defaults.baseURL = 'https://prod-test-sales-api.huoxingy.com/api/v1/sales/';
        break;
    default:
        axios.defaults.baseURL = 'https://sales-api.huoxingy.com/api/v1/sales/';
        break;
}
//



// axios.defaults.baseURL = 'https://staging-sales.huoxingy.com/api/v1/sales/';
// axios.defaults.baseURL = 'http://192.168.0.106:9090/v1/clerk/';
axios.defaults.headers.post['Content-Type'] = 'application/json';

/*
    设置请求拦截器
*/
axios.interceptors.request.use(function(config) {
    // console.log(config);

    // 获取验证码，注册不能带token验证
    /*if ( config.url.indexOf('v1/verify-code') >= 0 || config.url.indexOf('v1/admin/sign-up') >= 0 ) {
        return config;
    }*/
    let token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
}, function(error) {
    return Promise.reject(error);
});

/*
    添加响应拦截器
*/
axios.interceptors.response.use(function(response) {
    return response;
}, function(error) {
    if (error.response) {
        let tipText;
        error.response.data.message?tipText=error.response.data.message:tipText=error.response.data;
        if ( error.response.status == 401 ) {
            window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '帐号失效，请重新登录';
        } else if ( error.response.status == 403 ) {
            window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '权限不足';
        }
        Message.error({
            message: tipText,
            type: 'error'
        })
    }
    return Promise.reject(error);
});


/*
    添加自定义过滤器
*/
Vue.filter('numberToDate', function ( val ) {
    let dateStr = new Date(val).toLocaleDateString();
    return dateStr;
})
Vue.filter('numberToAllDate', function ( val ) {
    let date = new Date(val);
    let dateStr =date.getFullYear()+'/'+((date.getMonth()+1)<10?'0'+(date.getMonth()+1):(date.getMonth()+1))+'/'+(date.getDate()<10?'0'+date.getDate():date.getDate())+'-'+ 
    (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes())+':' + (date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds());
    return dateStr;
})

Vue.filter('numberToDate', function ( val ) {
    if ( !val ) {
        return '';
    }
    let date = new Date(val);
    let dateStr = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
    return dateStr;
})

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

Vue.filter('dateForGradePage', function (value) {
    if (!value) return '暂无'
    let date = new Date(value);
    let year = date.getFullYear();
    let month = (date.getMonth() + 1)>9?(date.getMonth() + 1):"0"+(date.getMonth() + 1);
    let day = date.getDate()>9?date.getDate():"0"+date.getDate();
    let hours = date.getHours();
    let minutes = date.getMinutes()>9?date.getMinutes():"0"+date.getMinutes();
    let seconds = date.getSeconds()>9?date.getSeconds():"0"+date.getSeconds();
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  })

Vue.prototype.$uuid = UUID
/*
  生产UUID：this.$uuid.create(1)
*/

Vue.prototype.$http = axios

Vue.prototype.$message = Message
Vue.prototype.$confirm = MessageBox
Vue.use(DatePicker);
Vue.use(Pagination);
Vue.use(Loading.directive);
Vue.use(Dialog);
Vue.use(Button);
Vue.use(Input);
Vue.use(Select);
Vue.use(Option);
Vue.use(Switch);
Vue.use(Checkbox);
Vue.use(Autocomplete);
Vue.use(Form);
Vue.use(FormItem);



// 创建滚动条方法
Vue.prototype.$createScroll = function ( id ) {
    var myScroll;
    myScroll = new iScroll( id, {
        scrollbarClass: 'scrollBar',
        hScroll: false,
        checkDOMChanges: true
    });
}

Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
    console.log(to)
    if(to.fullPath == "/call" || 
    to.fullPath == "/withDraw"|| to.fullPath == "/gift"|| 
    to.fullPath == "/clone" || to.fullPath == "/team" || 
    to.fullPath == "/storeMana/goodsMana" || 
    to.fullPath == "/storeMana/typeMana" || 
    to.fullPath == "/storeMana/specMana" ||
    to.fullPath == "/storeMana/orderMana"){
        if(sessionStorage.getItem("userData")){
            let userData = JSON.parse(sessionStorage.getItem("userData"));
            if((userData.mobile=="13980547109" && userData.name=="姜瑶")||userData.mobile=="18482220260"){
                if(userData.mobile=="18482220260"){
                    if(to.fullPath == "/call" || 
                    to.fullPath == "/withDraw"|| to.fullPath == "/gift"|| 
                    to.fullPath == "/clone" || to.fullPath == "/team" ){
                        return next({path: "/login"});
                    }
                }
                return next();
            }else{
                return next({path: "/store"});
            }
        }else{
            return next({path: "/store"});
        }
    }
    next();
});

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})
